#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "InfrTrackSensor.h"
#include "PhotoelectricSensor.h"
#include "Timer.h"
#include "HCSR04.h"
#include "Motor.h"
#include "Servo.h"
#include "Bluetooth.h"
#include "Key.h"
//#include "PID.h"
#include "IR.h"
#include "LED.h"

/*****************ty666****************/

/*蓝牙，超声波，红外遥控，红外寻迹这几个控制电机状态的功能同一时间只能有一个起作用，否则会导致各个功能之间起冲突(蓝牙和红外遥控可以同时作用)*/

/*本代码中除了未注释的部分，其余均为调试信息和解释，如果某个模块功能异常可解除相应测试代码的注释进行检测*/

uint8_t KeyNum,KeyState=2,KeyFlag;
uint8_t MFlag;
SetMotor_Speed MSpeed;

int main(void)
{	
	Timer_Init();
	Key_Init();
	
	while(1)
	{	            
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			KeyState+=1;
			if(KeyState>=3)
			{
				KeyState=1;
			}
			KeyFlag=1;
		}
		
		if(KeyState==1&&KeyFlag==1)
		{
			KeyFlag=0;
			MFlag=1;
			
			Key_Init();
			OLED_Init();
			LED_Init();
			IR_Init();  
			HCSR04_Init();
			Motor_Init();
//			Servo_Init();
			Bluetooth_Init();
			InfrTrackSensor_Init();
//			PhotoelectricSensor_Init();	 //光电传感器一次性测4个电机会出现问题（暂找不到原因），你可以尝试下能不能解决
			
			TIM_Cmd(TIM2,ENABLE);
			TIM_Cmd(TIM3,ENABLE);
			OLED_ShowString(2,4,"TYCAR OK!");
			Delay_ms(500);
			OLED_ShowString(2,4,"         ");
			OLED_ShowString(1,2,"ty_MasterCar");
			OLED_ShowString(2,1,"S1:");
			OLED_ShowString(2,9,"S2:");
			OLED_ShowString(3,1,"S3:");
			OLED_ShowString(3,9,"S4:");
			OLED_ShowString(4,9,"D:");
			OLED_ShowString(4,14,"cm");	
			
		}
		else if(KeyState==2)
		{
			OLED_Clear();
			OLED_ShowString(2,4,"TYCAR OFF!");
			Delay_ms(500);
			OLED_Clear();
			
			MFlag=0;
			MSpeed.Speed1=0;
			MSpeed.Speed2=0;
			MSpeed.Speed3=0;
			MSpeed.Speed4=0;
			Motor_SetAllSpeed(&MSpeed);
			
			TIM_Cmd(TIM2,DISABLE);
			TIM_Cmd(TIM3,DISABLE);
			
			GPIO_DeInit(GPIOA);
			GPIO_DeInit(GPIOB);
			GPIO_DeInit(GPIOC);
		}
		if(MFlag==1&&Bluetooth_GetFlag(BLUE_CTRCAR_FLAG)==0&&IR_GetFlag(IR_CTRCAR_FLAG)==0)
		{
			HCSR04_CtrMasterCar(&MSpeed);
		}
		
		IR_CtrMasterCar(&MSpeed);  //红外接收到数据后，对接受到的数据进行处理以控制小车
		Bluetooth_CtrMasterCar(&MSpeed);
		if(Infr_GetFlag())
		{
			NVIC_InitTypeDef NVIC_InitStructure;						//定义结构体变量
			NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;				//选择配置NVIC的TIM2线
			NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;				//指定NVIC线路使能
			NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;	//指定NVIC线路的抢占优先级为2
			NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;			//指定NVIC线路的响应优先级为1
			NVIC_Init(&NVIC_InitStructure);
					
			Infr_SetFlag(INFR_RESET);
			Infr_AdjustCar(&MSpeed);
			Motor_SetAllSpeed(&MSpeed);
		}
		NVIC_InitTypeDef NVIC_InitStructure;						//定义结构体变量
		NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;				//选择配置NVIC的TIM2线
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;				//指定NVIC线路使能
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;	//指定NVIC线路的抢占优先级为2
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;			//指定NVIC线路的响应优先级为1
		NVIC_Init(&NVIC_InitStructure);
		
		OLED_ShowSignedNum(2,4,MSpeed.Speed1,3);  //显示各个电机的实际速度
		OLED_ShowSignedNum(2,12,MSpeed.Speed2,3);
		OLED_ShowSignedNum(3,4,MSpeed.Speed3,3);
		OLED_ShowSignedNum(3,12,MSpeed.Speed4,3);
		
		OLED_ShowNum(4,11,HCSR04_GetDistace(),3);  //显示超声波测得的距离
		
		if(Bluetooth_GetFlag(BLUE_CTRCAR_FLAG))
		{
			OLED_ShowString(4,1,"BCTR OK!");
			Delay_ms(100);
			OLED_ShowString(4,1,"        ");
		}
		if(IR_GetFlag(IR_CTRCAR_FLAG))
		{
			OLED_ShowString(4,1,"IR OK!");
			Delay_ms(100);
			OLED_ShowString(4,1,"      ");
		}
		if(MSpeed.Speed1!=0||MSpeed.Speed2!=0||MSpeed.Speed3!=0||MSpeed.Speed4!=0)  //电机有速度就点亮一个LED（好看）
		{
			LED_ON();
		}
		else
		{
			LED_OFF();
		}
	}	
}

/*每隔0.1ms产生一次中断*/
void TIM1_UP_IRQHandler()
{
    static uint16_t Count2=0;
	if(TIM_GetITStatus(TIM1,TIM_IT_Update)==SET)
	{
		Count2++;
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==1)
		{
			Timer++;  //每产生一次中断计数加+1
		}
		if(Count2>=200)
		{
			Count2=0;
			Key_Loop();  //扫描按键
		}
		TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
	}
}

/*通过进入中断置标志位在主循环中来调控小车的速度，另外可以将左右两边的各两个传感器模块分开（就不测中间检测到黑线的情况），可以增强寻迹效果*/
void EXTI4_IRQHandler()
{
    if(EXTI_GetITStatus(EXTI_Line4) == SET&&IR_GetFlag(IR_CTRCAR_FLAG)!=1&&Bluetooth_GetFlag(BLUE_CTRCAR_FLAG)!=1&&HCSR04_GetFlag(HC_CHECK_FLAG)!=1)
    {
		Infr_SetFlag(INFR_SET);

        EXTI_ClearITPendingBit(EXTI_Line4);
    }
}

void EXTI9_5_IRQHandler()                         
{
	if(EXTI_GetITStatus(EXTI_Line4)==SET||EXTI_GetITStatus(EXTI_Line5)==SET||EXTI_GetITStatus(EXTI_Line6)==SET||((EXTI_GetITStatus(EXTI_Line7)==SET)
		&&IR_GetFlag(IR_CTRCAR_FLAG)!=1&&Bluetooth_GetFlag(BLUE_CTRCAR_FLAG)!=1&&HCSR04_GetFlag(HC_CHECK_FLAG)!=1))
	{
		Infr_SetFlag(INFR_SET);
		
		EXTI_ClearITPendingBit(EXTI_Line5|EXTI_Line6|EXTI_Line7);
	}
}
